﻿package net.wangit.view.element.component;

import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.util.Html;

public class ChartElementWriter extends AbstractElementWriter {

	public ChartElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row, true);
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	protected void writeInvisibleControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeControl(ac, html, ec, row, false);
	}

	protected void writeHidden(ActionContext actioncontext,
			StringBuffer stringbuffer, ElementComponent elementcomponent,
			Row row1) {
	}

	private void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean visible) {
		String chartId = (String) ec.getObject();
		if (!Checker.isEmpty(chartId)) {
			html.append("<img id=\"").append(ElementUtil.getAlias(ec)).append(
					"\"");
			if (!visible)
				html.append(" style=\"display:none\"");
			html.append(" src=\"/chart?domain=").append(
					ec.getParent().getDomain()).append("&uid=").append(
					ec.getParent().getId()).append("&eid=").append(ec.getId())
					.append("\"");
			Html.setCss(html, ec.getCss());
			Html.setTitle(html, ElementUtil.resolveTitle(ac, ec, row));
			html.append("/>");
		}
	}
}
